package it.storm.solution;

/**
 * 509. 斐波那契数
 * https://leetcode-cn.com/problems/fibonacci-number/
 */
public class Solutions_509 {
    public static void main(String[] args) {
//        int num = 0;  // output: 0

//        int num = 2;  // output: 1
//        解释：F(2) = F(1) + F(0) = 1 + 0 = 1

//        int num = 3;  // output: 2
//        解释：F(3) = F(2) + F(1) = 1 + 1 = 2

        int num = 4;  // output: 3
//        解释：F(4) = F(3) + F(2) = 2 + 1 = 3

        int result = fib(num);
        System.out.println(result);
    }

    /**
     * 解法二：递归
     */
    public static int fib(int n) {
        if (n <= 1) {
            // 递归结束条件
            return n;
        }
        // 深入递归
        return fib(n - 1) + fib(n - 2);
    }

    /**
     * 解法一：数组
     */
    public static int fib2(int n) {
        if (n == 0 || n == 1) {
            return n;
        }
        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            dp[i] += dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }
}
